import { _addressRegionList, _addressNew, _addressDetail, _addressDelete } from "../../../service/api";

Page({
  data: {
    addressId: 0,
    address: {
      id: 0,
      province_id: 0, // 省ID
      city_id: 0, // 市ID
      district_id: 0, // 区ID
      name: "", // 姓名
      mobile: null, // 手机
      address: "", // 详细地址
      is_default: 0, // 是否默认
      full_region: "", // 地址
    },
    scrollY: 0,
    isShowRegion: false, // 是否显示区域框
    regionType: 1, // 1省  2市  3区
    regionList: [], // 省市区列表
    regionDone: false, // 省市区完成状态
    selectRegionId: 0, // 当前的区域ID
    // 选择的省市区
    selectRegionList: [
      { id: 0, name: "省份", parent_id: 1, type: 1 },
      { id: 0, name: "城市", parent_id: 1, type: 2 },
      { id: 0, name: "区县", parent_id: 1, type: 3 },
    ],
  },
  onLoad: function (options) {
    if (options.id) {
      this.setData({ addressId: options.id });
      this.getAddressDetail();
    }
    this.getRegionList(1);
  },
  handleInputName(e) {
    const { value } = e.detail;
    this.setData({ "address.name": value });
  },
  handleInputMobile(e) {
    const { value } = e.detail;
    this.setData({ "address.mobile": value });
  },
  handleInputAddress(e) {
    const { value } = e.detail;
    this.setData({ "address.address": value });
  },
  handleIsDefault() {
    let address = this.data.address;
    address.is_default = address.is_default === 0 ? 1 : 0;
    this.setData({ address });
  },
  // 显示地址框
  handleChooseRegion() {
    this.setData({ isShowRegion: true });
    const { address, selectRegionList } = this.data;
    if (address.province_id > 0 && address.city_id > 0 && address.district_id > 0) {
      selectRegionList[0].id = address.province_id;
      selectRegionList[0].name = address.province_name;
      selectRegionList[0].parent_id = 1;

      selectRegionList[1].id = address.city_id;
      selectRegionList[1].name = address.city_name;
      selectRegionList[1].parent_id = address.province_id;

      selectRegionList[2].id = address.district_id;
      selectRegionList[2].name = address.district_name;
      selectRegionList[2].parent_id = address.city_id;
      this.setData({ selectRegionList, regionType: 3 });
    }
    this.setRegionDoneStatus();
  },
  // 切换省市区
  handleChangeRegionType(e) {
    const index = e.target.dataset.regionTypeIndex;
    let { selectRegionList, regionType } = this.data;
    let selectRegionItem = selectRegionList[index - 1];

    // 判断是否可以点击
    if (index + 1 === regionType || (index - 1 >= 0 && selectRegionItem.id === 0)) {
      return false;
    }
    selectRegionItem = selectRegionList[index];
    this.setData({ regionType: index + 1 });
    this.getRegionList(selectRegionItem.parent_id);
    this.setRegionDoneStatus();
  },
  // 选择省市区
  handleSelectRegionItem(e) {
    const regionIndex = e.target.dataset.regionIndex;
    let { regionList, selectRegionList, regionType } = this.data;
    const regionItem = regionList[regionIndex];
    let selectRegionType = regionItem.type;
    // 把默认省市区替换成选择的
    selectRegionList[selectRegionType - 1] = regionItem;

    // 如果还没选到最后一级, 则发送请求获取下一级的地址
    if (selectRegionType !== 3) {
      this.setData({ regionType: selectRegionType + 1, selectRegionId: regionItem.id });
      this.getRegionList(regionItem.id);
    }

    // 如果用户切换省市区之后又从新选择, 则需要重置下级的区域
    selectRegionList.map((item, index) => {
      if (index > regionType - 1) {
        item.id = 0;
        item.name = index == 1 ? "城市" : "区县";
        item.parent_id = this.data.selectRegionId;
      }
      return item;
    });

    // 把选择的标记起来
    regionList.map((item) => {
      if (regionType == item.type && selectRegionList[regionType - 1].id == item.id) {
        item.selected = true;
      } else {
        item.selected = false;
      }
      return item;
    });
    this.setData({ selectRegionList, regionList, scrollY: 0 });
    this.setRegionDoneStatus();
  },
  // 确认地址
  handleConfirmRegion() {
    const { regionDone, address, selectRegionList } = this.data;
    if (regionDone === false) return false;
    address.province_id = selectRegionList[0].id;
    address.province_name = selectRegionList[0].name;
    address.city_id = selectRegionList[1].id;
    address.city_name = selectRegionList[1].name;
    address.district_id = selectRegionList[2].id;
    address.district_name = selectRegionList[2].name;
    address.full_region = selectRegionList.map((item) => item.name).join("");
    this.setData({ address, isShowRegion: false });
  },
  // 隐藏地址框
  handleHideRegion() {
    this.setData({ isShowRegion: false });
  },
  // 保存
  handleSaveAddress() {
    const address = this.data.address;
    const reg = /^(13|14|15|18)\d{9}$/;
    if (address.name.trim() == "") {
      return wx.showToast({ title: "请输入姓名", icon: "error", duration: 1000, mask: true });
    }
    if (!address.mobile) {
      return wx.showToast({ title: "请输入手机号码", icon: "error", duration: 1000, mask: true });
    }
    if (!reg.test(address.mobile)) {
      return wx.showToast({ title: "手机号码格式错误", icon: "error", duration: 1000, mask: true });
    }
    if (address.district_id === 0) {
      return wx.showToast({ title: "请输入省市区", icon: "error", duration: 1000, mask: true });
    }
    if (address.address.trim() == "") {
      return wx.showToast({ title: "请输入详细地址", icon: "error", duration: 1000, mask: true });
    }
    _addressNew(address).then((res) => {
      if (res.errno === 200) {
        wx.navigateBack({
          delta: 1,
        });
      }
    });
  },
  // 取消
  handleCancelAddress() {
    wx.navigateBack({
      delta: 1,
    });
  },
  // 删除
  handleDeleteAddress() {
    _addressDelete(this.data.addressId).then((res) => {
      wx.navigateBack({
        delta: 1,
      });
    });
  },
  // 获取区域列表
  async getRegionList(id) {
    const { regionType, selectRegionList } = this.data;
    const res = await _addressRegionList({ parentId: id });
    const regionList = res.region.map((item) => {
      if (item.type == regionType && item.id == selectRegionList[regionType - 1].id) {
        item.selected = true;
      } else {
        item.selected = false;
      }
      return item;
    });
    this.setData({ regionList });
  },
  // 获取地址详细
  async getAddressDetail() {
    const res = await _addressDetail({ id: this.data.addressId });
    this.setData({ address: res.data });
  },
  // 设置区域完成状态
  setRegionDoneStatus() {
    const flag = this.data.selectRegionList.every((item) => item.id !== 0);
    this.setData({ regionDone: flag });
  },
});
